/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * Command to create instances of Comments.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Owner - any Element
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>Comment
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class CreateCommentCommand extends Command {

	private static final String DEFAULT_COMMENT = "Note";
	private Element owner;
	private Comment comment;

	public void setOwner(final Element owner) {
		this.owner = owner;
	}

	public Comment getComment() {
		return comment;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		comment = UMLFactory.eINSTANCE.createComment();
		comment.setBody(DEFAULT_COMMENT);

		owner.getOwnedComments().add(comment);
	}

	@Override
	public void undo() {
		owner.getOwnedComments().remove(comment);
	}

	@Override
	public void redo() {
		owner.getOwnedComments().add(comment);
	}

	@Override
	public void dispose() {
		comment = null;
		owner = null;
	}
}
